/*
 * QColorAdapter.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QColorAdapter.h"

#include "JNISwingWrapper.h"

// QColorAdapter :: Public Constructors. ---------------------------------------

/*!
 * \brief Constructs new QColorAdapter object.
 *
 * \param color the Swing color
 */
QColorAdapter::QColorAdapter(jobject color)
  : QColor()
{
  jint argb = 0;
  if (JNISwingWrapper::callIntMethod(argb, color, sm_getARGBMID)) {
    setRgb(0xFF & (argb >> 16), 0xFF & (argb >> 8), 0xFF & argb,
           0xFF & (argb >> 24));
  }
}

/*!
 * \brief Constructs new QColorAdapter object.
 *
 * \param argb the argb value of the color
 */
QColorAdapter::QColorAdapter(jint argb)
  : QColor(0xFF & (argb >> 16), 0xFF & (argb >> 8), 0xFF & argb,
           0xFF & (argb >> 24))
{
  // empty constructor body
}

// QColorAdapter :: Public Static Member Functions. ----------------------------

/*!
 * \brief Initializes QColorAdapter class.
 *
 * \param env the JNIEnv pointer
 * \param cls the Swing color class
 *
 * \exception FatalError if failed
 */
void QColorAdapter::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_colorClass);
  if (!INIT_CLASS(color, cls) ||
      !INIT_CONSTRUCTOR(constructor, cls, "(IIII)V") ||
      !INIT_MID(getARGB, cls, "()I")) {
    FATAL_LOGGED_ERROR("QColorAdapter initialization failed");
  }
}

/*!
 * \brief Creates new Swing color instance.
 *
 * \param env the JNIEnv pointer
 * \param colorClass the Swing color class
 * \param color the QColor reference
 *
 * \return new Swing color instance
 *
 * \exception OutOfMemoryError java exception
 */
jobject QColorAdapter::toJColor(JNIEnv *env, jclass colorClass,
                                const QColor &color)
{
  return JNIWrapper::newObject(env, colorClass, sm_constructorMID, color.red(),
                               color.green(), color.blue(), color.alpha());
}

/*!
 * \brief Creates new Swing color instance.
 *
 * \param color the QColor reference
 *
 * \return new Swing color instance
 *
 * \exception OutOfMemoryError java exception
 */
jobject QColorAdapter::toJColor(const QColor &color)
{
  return JNISwingWrapper::newObject(sm_colorClass, sm_constructorMID,
                                    color.red(), color.green(), color.blue(),
                                    color.alpha());
}

// QColorAdapter :: Private Static Data Members. -------------------------------

jclass QColorAdapter::sm_colorClass = 0;
jmethodID QColorAdapter::sm_constructorMID = 0;
jmethodID QColorAdapter::sm_getARGBMID = 0;
